<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\models\Address;
use App\models\Bale;
use App\models\Cart;
use App\models\Goods;
use App\models\Order;
use App\models\OrderDetail;
use Illuminate\Http\Request;

class OrderController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,Order $order)
    {
//       dd(123);
//        dd($request->all());
        $ids = explode(',',$request->ids);
//        dd($ids);
        $zongjia = Cart::whereIn('id',$ids)->sum('xiaoji');
//        写入订单表
        $order->total_price = $zongjia;
        $order->user_id = auth()->id();
        $order->address_id = $request->address_id;
        $order->order_num = time().str_random(10);
//        写入订单详情表
        $order->save();
//        购物车数据
        $carts = Cart::whereIn('id',$ids)->get();
        foreach ($carts as $cart)
        {
          $orderDetail = new OrderDetail();
          $orderDetail->order_id = $order->id;
          $orderDetail->good_id = $cart->goods_id;
          $orderDetail->bale_id = $cart->bale_id;
          $orderDetail->num = $cart->num;
          $orderDetail->save();
        }
        return ['code'=>1,'message'=>'订单生成成功','data'=>[
            'order_num'=>$order->order_num
        ]];
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\models\Order  $order
     * @return \Illuminate\Http\Response
     */
    public function show(Order $order)
    {
//       dd(123);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\models\Order  $order
     * @return \Illuminate\Http\Response
     */
    public function edit(Order $order)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\models\Order  $order
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Order $order)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\models\Order  $order
     * @return \Illuminate\Http\Response
     */
    public function destroy(Order $order)
    {
//        dd(123);
        OrderDetail::where('order_id',$order['id'])->delete();
        $order->delete();
        return back()->with('success','删除订单成功');
    }

    public function lists($ids)
    {
//        dd($ids);
//        获取当前登录用户的所有地址
        $addresses = Address::where('user_id',auth()->id())->get();
        $order_ids = rtrim($ids,',');
        $ids = explode(',',$order_ids);
//        dd($ids);
        $carts = Cart::whereIn('id',$ids)->get();
        $zongjia = Cart::whereIn('id',$ids)->sum('xiaoji');
        $num = Cart::whereIn('id',$ids)->sum('num');
        return view('home.order_lists',compact('addresses','carts','zongjia','order_ids','num'));
    }

//    在商品详情页直接购买货品方法
    public function buy(Bale $huopin,Request $request)
    {
//        dd(123);
//        dd($request->all());
//        dd($huopin);
        $data = $request->all();
        $num = $data['num'];
//        dd($num);
        $price = $data['price'];
//        dd($price);
        $zongjia = 0;
//        for ($num;$num>0;$num--)
//        {
//            $zongjia = $zongjia + $price;
//        }
        $zongjia = $price * $num;
//        dd($zongjia);
        $addresses = Address::where('user_id',auth()->id())->get();
       return view('home.order_buy',compact('addresses','zongjia','num','huopin'));
    }
//    直接购买，生成订单表和订单详情表
    public function storeOne(Request $request,Order $order)
    {
        $shangpin = $request->shangpin;
//        dd($shangpin);
        $zongjia = $request->zongjia;
//        写入订单表
        $order->total_price = $zongjia;
        $order->user_id = auth()->id();
        $order->address_id = $request->address_id;
        $order->order_num = time().str_random(10);
        //        写入订单详情表
        $order->save();
//        dd($order);
        $orderDetail = new OrderDetail();
        $orderDetail->order_id = $order->id;
        $orderDetail->good_id = $shangpin;
        $orderDetail->bale_id = $request['huopin'];
        $orderDetail->num = $request->num;
        $orderDetail->save();
        return ['code'=>1,'message'=>'订单生成成功','data'=>[
            'order_num'=>$order->order_num
        ]];
    }

//    查看各个状态的订单
   public function orderStatus($order_status)
   {
//           dd($order_status);
       $orders = Order::where('user_id', auth()->id())->where('status', $order_status)->get();
//       dd($orders);
       return view('home.order_status', compact('orders','order_status'));
   }
}
